package com.uphone.kingmall.main;

import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.china.zhuoyue.R;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.activity.SearchGoodsListActivity;
import com.uphone.kingmall.adapter.HomeAdapter;
import com.uphone.kingmall.adapter.MenuAdapter;
import com.uphone.kingmall.base.BaseFragment;
import com.uphone.kingmall.bean.CatsListBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/02/15
 * desc   : 分类fragment
 */
public class ClassFragment0 extends BaseFragment {
    @BindView(R.id.lv_menu)
    ListView lvMenu;
    @BindView(R.id.lv_home)
    ListView lvHome;
    @BindView(R.id.lv_home2)
    RecyclerView lvHome2;
    @BindView(R.id.iv_sousuo)
    ImageView ivSousuo;
    @BindView(R.id.rl_sousuo)
    RelativeLayout rlSousuo;
    //    @BindView(R.id.shop_cart_refresh)
//    SwipeRefreshLayout shopCartRefresh;
    private HomeAdapter homeAdapter;
    private MenuAdapter menuAdapter;
    private int currentItem;
    private List<Integer> showTitle = new ArrayList<>();
    private ArrayList<CatsListBean.CatsBean> catsList;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_fragment_class;
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }

    @Override
    public void initData() {
        homeAdapter = new HomeAdapter(getActivitySafely(), catsList);
        lvHome.setAdapter(homeAdapter);
        menuAdapter = new MenuAdapter(getActivitySafely(), catsList);
        lvMenu.setAdapter(menuAdapter);
        lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                menuAdapter.setSelectItem(position);
                menuAdapter.notifyDataSetInvalidated();
                lvHome.setSelection(showTitle.get(position));
            }
        });

        lvHome.setOnScrollListener(new AbsListView.OnScrollListener() {
            private int scrollState;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                this.scrollState = scrollState;
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    return;
                }
                int current = showTitle.indexOf(firstVisibleItem);
//				lv_home.setSelection(current);
                if (currentItem != current && current >= 0) {
                    currentItem = current;
                    menuAdapter.setSelectItem(currentItem);
//                    menuAdapter.
//                    lvMenu.smoothScrollToPosition();
                    lvMenu.setSelection(currentItem);
                    menuAdapter.notifyDataSetInvalidated();
                }
            }
        });

//        shopCartRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        shopCartRefresh.setRefreshing(false);
//                    }
//                }, 2000);
//                loadData();
//            }
//        });
//        loadData();
    }

    private void loadData() {
        OkGoUtils.normalRequest(MyUrl.getCatsList, null, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                final CatsListBean catsListBean = GsonUtils.getGson().fromJson(body, CatsListBean.class);
                if (catsListBean.getCode() == 0) {
                    if (catsList == null) {
                        catsList = new ArrayList<>();
                    }
                    catsList.clear();
                    showTitle.clear();
                    if (catsListBean.getCats() != null) {
                        catsList.addAll(catsListBean.getCats());
                        for (int i = 0; i < catsListBean.getCats().size(); i++) {
                            showTitle.add(i);
                        }
                    }
                    homeAdapter.setList(catsList);
                    menuAdapter.setList(catsList);
                }
            }

            @Override
            public void onError(Response<String> response) {
            }
        });
    }

    @Override
    public void initView(View view) {

    }

    @OnClick({R.id.iv_sousuo, R.id.rl_sousuo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_sousuo:
                break;
            case R.id.rl_sousuo:
                startActivity(new Intent(getActivity(), SearchGoodsListActivity.class));
                break;
        }
    }
}
